package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apiario;
import com.model.Apicultor;
import com.model.Exame;
import com.model.Medicamentos;
import com.model.Pragas;

import com.model.RegPragas;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table REG_PRAGAS.
*/
public class RegPragasDao extends AbstractDao<RegPragas, Long> {

    public static final String TABLENAME = "REG_PRAGAS";

    /**
     * Properties of entity RegPragas.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, long.class, "id", true, "_idRegP");
        public final static Property Quarentena = new Property(1, Integer.class, "quarentena", false, "QUARENTENA");
        public final static Property NomeEnxame = new Property(2, String.class, "nomeEnxame", false, "NOME_ENXAME");
        public final static Property Data = new Property(3, String.class, "data", false, "DATA");
        public final static Property Descricao = new Property(4, String.class, "descricao", false, "DESCRICAO");
        public final static Property Veternario = new Property(5, String.class, "veternario", false, "VETERNARIO");
        public final static Property Tratamento = new Property(6, Boolean.class, "tratamento", false, "TRATAMENTO");
        public final static Property Abate = new Property(7, Boolean.class, "Abate", false, "ABATE");
        public final static Property Dataretirar = new Property(8, java.util.Date.class, "dataretirar", false, "DATARETIRAR");
        public final static Property Retirado = new Property(9, Boolean.class, "retirado", false, "RETIRADO");
        public final static Property Dataretirado = new Property(10, java.util.Date.class, "dataretirado", false, "DATARETIRADO");
        public final static Property MedicamentoId = new Property(11, long.class, "medicamentoId", false, "MEDICAMENTO_ID");
        public final static Property PragaId = new Property(12, long.class, "pragaId", false, "PRAGA_ID");
        public final static Property ApicultorId = new Property(13, long.class, "apicultorId", false, "APICULTOR_ID");
        public final static Property ApiarioId = new Property(14, long.class, "apiarioId", false, "APIARIO_ID");
        public final static Property ExameId = new Property(15, long.class, "exameId", false, "EXAME_ID");
        public final static Property Date = new Property(16, java.util.Date.class, "date", false, "DATE");
    };

    private DaoSession daoSession;

    private Query<RegPragas> apiario_ApiarioRegPragasQuery;
    private Query<RegPragas> apicultor_ApicultorRegPragasQuery;
    private Query<RegPragas> exame_ExameRegPragasQuery;
    private Query<RegPragas> pragas_PragaRegPragasQuery;
    private Query<RegPragas> medicamentos_MedicamentoRegPragasQuery;

    public RegPragasDao(DaoConfig config) {
        super(config);
    }
    
    public RegPragasDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'REG_PRAGAS' (" + //
                "'_idRegP' INTEGER PRIMARY KEY ASC AUTOINCREMENT NOT NULL ," + // 0: id
                "'QUARENTENA' INTEGER," + // 1: quarentena
                "'NOME_ENXAME' TEXT," + // 2: nomeEnxame
                "'DATA' TEXT," + // 3: data
                "'DESCRICAO' TEXT," + // 4: descricao
                "'VETERNARIO' TEXT," + // 5: veternario
                "'TRATAMENTO' INTEGER," + // 6: tratamento
                "'ABATE' INTEGER," + // 7: Abate
                "'DATARETIRAR' INTEGER," + // 8: dataretirar
                "'RETIRADO' INTEGER," + // 9: retirado
                "'DATARETIRADO' INTEGER," + // 10: dataretirado
                "'MEDICAMENTO_ID' INTEGER NOT NULL ," + // 11: medicamentoId
                "'PRAGA_ID' INTEGER NOT NULL ," + // 12: pragaId
                "'APICULTOR_ID' INTEGER NOT NULL ," + // 13: apicultorId
                "'APIARIO_ID' INTEGER NOT NULL ," + // 14: apiarioId
                "'EXAME_ID' INTEGER NOT NULL ," + // 15: exameId
                "'DATE' INTEGER);"); // 16: date
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'REG_PRAGAS'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, RegPragas entity) {
        stmt.clearBindings();
        stmt.bindLong(1, entity.getId());
 
        Integer quarentena = entity.getQuarentena();
        if (quarentena != null) {
            stmt.bindLong(2, quarentena);
        }
 
        String nomeEnxame = entity.getNomeEnxame();
        if (nomeEnxame != null) {
            stmt.bindString(3, nomeEnxame);
        }
 
        String data = entity.getData();
        if (data != null) {
            stmt.bindString(4, data);
        }
 
        String descricao = entity.getDescricao();
        if (descricao != null) {
            stmt.bindString(5, descricao);
        }
 
        String veternario = entity.getVeternario();
        if (veternario != null) {
            stmt.bindString(6, veternario);
        }
 
        Boolean tratamento = entity.getTratamento();
        if (tratamento != null) {
            stmt.bindLong(7, tratamento ? 1l: 0l);
        }
 
        Boolean Abate = entity.getAbate();
        if (Abate != null) {
            stmt.bindLong(8, Abate ? 1l: 0l);
        }
 
        java.util.Date dataretirar = entity.getDataretirar();
        if (dataretirar != null) {
            stmt.bindLong(9, dataretirar.getTime());
        }
 
        Boolean retirado = entity.getRetirado();
        if (retirado != null) {
            stmt.bindLong(10, retirado ? 1l: 0l);
        }
 
        java.util.Date dataretirado = entity.getDataretirado();
        if (dataretirado != null) {
            stmt.bindLong(11, dataretirado.getTime());
        }
        stmt.bindLong(12, entity.getMedicamentoId());
        stmt.bindLong(13, entity.getPragaId());
        stmt.bindLong(14, entity.getApicultorId());
        stmt.bindLong(15, entity.getApiarioId());
        stmt.bindLong(16, entity.getExameId());
 
        java.util.Date date = entity.getDate();
        if (date != null) {
            stmt.bindLong(17, date.getTime());
        }
    }

    @Override
    protected void attachEntity(RegPragas entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public RegPragas readEntity(Cursor cursor, int offset) {
        RegPragas entity = new RegPragas( //
            cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1), // quarentena
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // nomeEnxame
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // data
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // descricao
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // veternario
            cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0, // tratamento
            cursor.isNull(offset + 7) ? null : cursor.getShort(offset + 7) != 0, // Abate
            cursor.isNull(offset + 8) ? null : new java.util.Date(cursor.getLong(offset + 8)), // dataretirar
            cursor.isNull(offset + 9) ? null : cursor.getShort(offset + 9) != 0, // retirado
            cursor.isNull(offset + 10) ? null : new java.util.Date(cursor.getLong(offset + 10)), // dataretirado
            cursor.getLong(offset + 11), // medicamentoId
            cursor.getLong(offset + 12), // pragaId
            cursor.getLong(offset + 13), // apicultorId
            cursor.getLong(offset + 14), // apiarioId
            cursor.getLong(offset + 15), // exameId
            cursor.isNull(offset + 16) ? null : new java.util.Date(cursor.getLong(offset + 16)) // date
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, RegPragas entity, int offset) {
        entity.setId(cursor.getLong(offset + 0));
        entity.setQuarentena(cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1));
        entity.setNomeEnxame(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setData(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setDescricao(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setVeternario(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setTratamento(cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0);
        entity.setAbate(cursor.isNull(offset + 7) ? null : cursor.getShort(offset + 7) != 0);
        entity.setDataretirar(cursor.isNull(offset + 8) ? null : new java.util.Date(cursor.getLong(offset + 8)));
        entity.setRetirado(cursor.isNull(offset + 9) ? null : cursor.getShort(offset + 9) != 0);
        entity.setDataretirado(cursor.isNull(offset + 10) ? null : new java.util.Date(cursor.getLong(offset + 10)));
        entity.setMedicamentoId(cursor.getLong(offset + 11));
        entity.setPragaId(cursor.getLong(offset + 12));
        entity.setApicultorId(cursor.getLong(offset + 13));
        entity.setApiarioId(cursor.getLong(offset + 14));
        entity.setExameId(cursor.getLong(offset + 15));
        entity.setDate(cursor.isNull(offset + 16) ? null : new java.util.Date(cursor.getLong(offset + 16)));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(RegPragas entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(RegPragas entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "apiarioRegPragas" to-many relationship of Apiario. */
    public List<RegPragas> _queryApiario_ApiarioRegPragas(long apiarioId) {
        synchronized (this) {
            if (apiario_ApiarioRegPragasQuery == null) {
                QueryBuilder<RegPragas> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ApiarioId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apiario_ApiarioRegPragasQuery = queryBuilder.build();
            }
        }
        Query<RegPragas> query = apiario_ApiarioRegPragasQuery.forCurrentThread();
        query.setParameter(0, apiarioId);
        return query.list();
    }

    /** Internal query to resolve the "apicultorRegPragas" to-many relationship of Apicultor. */
    public List<RegPragas> _queryApicultor_ApicultorRegPragas(long apicultorId) {
        synchronized (this) {
            if (apicultor_ApicultorRegPragasQuery == null) {
                QueryBuilder<RegPragas> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ApicultorId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apicultor_ApicultorRegPragasQuery = queryBuilder.build();
            }
        }
        Query<RegPragas> query = apicultor_ApicultorRegPragasQuery.forCurrentThread();
        query.setParameter(0, apicultorId);
        return query.list();
    }

    /** Internal query to resolve the "exameRegPragas" to-many relationship of Exame. */
    public List<RegPragas> _queryExame_ExameRegPragas(long exameId) {
        synchronized (this) {
            if (exame_ExameRegPragasQuery == null) {
                QueryBuilder<RegPragas> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ExameId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                exame_ExameRegPragasQuery = queryBuilder.build();
            }
        }
        Query<RegPragas> query = exame_ExameRegPragasQuery.forCurrentThread();
        query.setParameter(0, exameId);
        return query.list();
    }

    /** Internal query to resolve the "pragaRegPragas" to-many relationship of Pragas. */
    public List<RegPragas> _queryPragas_PragaRegPragas(long pragaId) {
        synchronized (this) {
            if (pragas_PragaRegPragasQuery == null) {
                QueryBuilder<RegPragas> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.PragaId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                pragas_PragaRegPragasQuery = queryBuilder.build();
            }
        }
        Query<RegPragas> query = pragas_PragaRegPragasQuery.forCurrentThread();
        query.setParameter(0, pragaId);
        return query.list();
    }

    /** Internal query to resolve the "medicamentoRegPragas" to-many relationship of Medicamentos. */
    public List<RegPragas> _queryMedicamentos_MedicamentoRegPragas(long medicamentoId) {
        synchronized (this) {
            if (medicamentos_MedicamentoRegPragasQuery == null) {
                QueryBuilder<RegPragas> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.MedicamentoId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                medicamentos_MedicamentoRegPragasQuery = queryBuilder.build();
            }
        }
        Query<RegPragas> query = medicamentos_MedicamentoRegPragasQuery.forCurrentThread();
        query.setParameter(0, medicamentoId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getApiarioDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getApicultorDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getExameDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T3", daoSession.getPragasDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T4", daoSession.getMedicamentosDao().getAllColumns());
            builder.append(" FROM REG_PRAGAS T");
            builder.append(" LEFT JOIN APIARIO T0 ON T.'APIARIO_ID'=T0.'_idApiario'");
            builder.append(" LEFT JOIN APICULTOR T1 ON T.'APICULTOR_ID'=T1.'_idApicultor'");
            builder.append(" LEFT JOIN EXAME T2 ON T.'EXAME_ID'=T2.'_idExame'");
            builder.append(" LEFT JOIN PRAGAS T3 ON T.'PRAGA_ID'=T3.'_idPraga'");
            builder.append(" LEFT JOIN MEDICAMENTOS T4 ON T.'MEDICAMENTO_ID'=T4.'_idMedicamento'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected RegPragas loadCurrentDeep(Cursor cursor, boolean lock) {
        RegPragas entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Apiario apiario = loadCurrentOther(daoSession.getApiarioDao(), cursor, offset);
         if(apiario != null) {
            entity.setApiario(apiario);
        }
        offset += daoSession.getApiarioDao().getAllColumns().length;

        Apicultor apicultor = loadCurrentOther(daoSession.getApicultorDao(), cursor, offset);
         if(apicultor != null) {
            entity.setApicultor(apicultor);
        }
        offset += daoSession.getApicultorDao().getAllColumns().length;

        Exame exame = loadCurrentOther(daoSession.getExameDao(), cursor, offset);
         if(exame != null) {
            entity.setExame(exame);
        }
        offset += daoSession.getExameDao().getAllColumns().length;

        Pragas pragas = loadCurrentOther(daoSession.getPragasDao(), cursor, offset);
         if(pragas != null) {
            entity.setPragas(pragas);
        }
        offset += daoSession.getPragasDao().getAllColumns().length;

        Medicamentos medicamentos = loadCurrentOther(daoSession.getMedicamentosDao(), cursor, offset);
         if(medicamentos != null) {
            entity.setMedicamentos(medicamentos);
        }

        return entity;    
    }

    public RegPragas loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<RegPragas> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<RegPragas> list = new ArrayList<RegPragas>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<RegPragas> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<RegPragas> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
